package com.mvvm.ktnet.utils

import android.app.Instrumentation
import android.os.SystemClock
import android.util.Log
import android.view.KeyEvent
import com.blankj.utilcode.util.LogUtils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

object KeyEventUtils {

    fun sendCode1(keyCode: Int) {
        GlobalScope.launch {
            withContext(Dispatchers.IO) {
                val keyCommand = "input keycode " + keyCode
                // 调用Runtime模拟按键操作
                Runtime.getRuntime().exec(keyCommand);
            }
        }
    }

    suspend fun sendCode2(keyCode: Int) {

//        GlobalScope.launch {
        withContext(Dispatchers.IO) {
            kotlin.runCatching {
                val inst = Instrumentation()
                // 调用inst对象的按键模拟方法
                inst.sendKeyDownUpSync(keyCode)
            }.onFailure {

//                Log.e("sendCode2", "sendCode2: ${it.message}")

                LogUtils.e("sendCode2: ${it.message}")
            }.onSuccess {


                LogUtils.e("sendCode2:成功")
            }
        }
    }
//}
}